const express = require('express')
const router = require('./router')
// const userRouter = require('./router/user')
// const videoRouter = require('./router/video')

const app = express()
const PORT = process.env.PORT || 3000

app.use(express.json())
app.use(express.urlencoded())
app.use(express.static('public')) //处理静态资源 方便直接访问public下的文件  如: http://127.0.0.1/xxx.jpg
app.use('/api/v1',router)

//路由错误处理中间件(内置)
app.use((req,res,next)=>{
   res.send(404,'404 not found!')
})


//错误处理中间件(内置)  (()=>{}接收的参数有四个 第一个是err)
app.use(function(err, req, res, next){
  console.error(err.stack);
  res.send(500, 'Something broke!'+err);
});

app.listen(PORT, () => {
  console.log('\x1B[32m%s\x1B[0m', `=============> Server is running at http://localhost:${PORT} <==============`)//终端打印颜色
})
